package com.aiyi.server.manager.nginx.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Nginx 配置工具类
 * @Project : nginx-gui
 * @Program Name : com.aiyi.server.manager.nginx.common.NginxUtils.java
 * @Description : 
 * @Author : 郭胜凯
 * @Creation Date : 2018年2月22日 下午6:20:18
 * @ModificationHistory Who When What ---------- ------------- -----------------------------------
 *                      郭胜凯 2018年2月22日 create
 *                      歪歪杰 2021年10月07日 重构，功能转移至 NginxService 类中
 */
public class NginxUtils {

	/**
	 * 判断字符是否是中文
	 *
	 * @param c 字符
	 * @return 是否是中文
	 */
	public static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		// CJK统一表意文字，CJK兼容性表意文字， CJK统一表意文字扩展A， 通用标点， CJK符号和标点符号，半角和全角表单
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符串是否是乱码
	 *
	 * @param strName 字符串
	 * @return 是否是乱码
	 */
	public static boolean isMessyCode(String strName) {
		char[] ch = strName.trim().toCharArray();
		int chLength = 0;
		int count = 0;
		
		Map<String, Integer> mCountMap = new HashMap<String, Integer>();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
			String ubName = ub.toString();
			// 基本的拉丁字母不用考虑
			if(!"BASIC_LATIN".equals(ubName)) {
				// 日语片假名、平假名合并统计
				if ("KATAKANA".equals(ubName)||"HIRAGANA".equals(ubName)) {
					ubName = "JAPANESE_KANA";
				}
				Integer mCount = mCountMap.get(ubName);
				if(mCount==null) {
					mCount = 1;
				}else {
					mCount = mCount+1;
				}
				mCountMap.put(ubName, mCount);
			}
		}
		int typeCount = mCountMap.size();
		for(Entry<String, Integer> mCount:mCountMap.entrySet()) {
			String key = mCount.getKey();
			if(!"CJK_UNIFIED_IDEOGRAPHS".equals(key)) {
				count += mCount.getValue();
			}
			chLength += mCount.getValue();
			System.out.println(mCount.getKey()+"："+mCount.getValue());
		}
		
		float result = chLength == 0 ? 0 : ((0f + count) / chLength);
		System.out.println("typeCount: "+typeCount+"\tresult："+result);
		// 类型多于2种或比例超过10%判定为乱码
		if (typeCount>2 || result > 0.1) {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) {
		System.out.println(isMessyCode("#user  nobody;\n"
				+ "worker_processes 1;\n"
				+ "#error_log  logs/error.log;\n"
				+ "#error_log  logs/error.log  notice;\n"
				+ "#error_log  logs/error.log  info;\n"
				+ "#pid        logs/nginx.pid;\n"
				+ "events {\n"
				+ "  worker_connections 1024;\n"
				+ "}\n"
				+ "http {\n"
				+ "  include mime.types;\n"
				+ "  default_type application/octet-stream;\n"
				+ "  #log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n"
				+ "  #                  '$status $body_bytes_sent \"$http_referer\" '\n"
				+ "  #                  '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n"
				+ "  #access_log  logs/access.log  main;\n"
				+ "  sendfile on;\n"
				+ "  #tcp_nopush     on;\n"
				+ "  #keepalive_timeout  0;\n"
				+ "  keepalive_timeout 65;\n"
				+ "  gzip  on;\n"
				+ "  server {\n"
				+ "    listen 80;\n"
				+ "    server_name localhost;\n"
				+ "    #charset koi8-r;\n"
				+ "    #access_log  logs/host.access.log  main;\n"
				+ "    location / {\n"
				+ "      root html;\n"
				+ "      index index.html index.htm;\n"
				+ "    }\n"
				+ "    #error_page  404              /404.html;\n"
				+ "    # 将服务器错误页面重定向到静态页面 /50x.html\n"
				+ "    #\n"
				+ "    error_page 500 502 503 504 /50x.html;\n"
				+ "    location = /50x.html {\n"
				+ "      root html;\n"
				+ "    }\n"
				+ "    # 将 PHP 脚本代理到 127.0.0.1:80 监听的 Apache 上\n"
				+ "    #\n"
				+ "    #location ~ \\.php$ {\n"
				+ "    #    proxy_pass   http://127.0.0.1;\n"
				+ "    #}\n"
				+ "    # 将 PHP 脚本传递给监听 127.0.0.1:9000 的 FastCGI 服务器\n"
				+ "    #\n"
				+ "    #location ~ \\.php$ {\n"
				+ "    #    root           html;\n"
				+ "    #    fastcgi_pass   127.0.0.1:9000;\n"
				+ "    #    fastcgi_index  index.php;\n"
				+ "    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;\n"
				+ "    #    include        fastcgi_params;\n"
				+ "    #}\n"
				+ "    # 拒绝访问 .htaccess 文件，如果Apache的文档根与nginx的一致\n"
				+ "    #\n"
				+ "    #location ~ /\\.ht {\n"
				+ "    #    deny  all;\n"
				+ "    #}\n"
				+ "  }\n"
				+ "  # 另一个虚拟主机，使用基于IP、名称和端口的配置\n"
				+ "  #\n"
				+ "  #server {\n"
				+ "  #    listen       8000;\n"
				+ "  #    listen       somename:8080;\n"
				+ "  #    server_name  somename  alias  another.alias;\n"
				+ "  #    location / {\n"
				+ "  #        root   html;\n"
				+ "  #        index  index.html index.htm;\n"
				+ "  #    }\n"
				+ "  #}\n"
				+ "  # HTTPS 服务器\n"
				+ "  #\n"
				+ "  #server {\n"
				+ "  #    listen       443 ssl;\n"
				+ "  #    server_name  localhost;\n"
				+ "  #    ssl_certificate      cert.pem;\n"
				+ "  #    ssl_certificate_key  cert.key;\n"
				+ "  #    ssl_session_cache    shared:SSL:1m;\n"
				+ "  #    ssl_session_timeout  5m;\n"
				+ "  #    ssl_ciphers  HIGH:!aNULL:!MD5;\n"
				+ "  #    ssl_prefer_server_ciphers  on;\n"
				+ "  #    location / {\n"
				+ "  #        root   html;\n"
				+ "  #        index  index.html index.htm;\n"
				+ "  #    }\n"
				+ "  #}\n"
				+ "}\n"
				+ ""));
		System.out.println(isMessyCode("#user  nobody;\n"
				+ "    # 閻忓繐妫欏﹢鍥礉閳ヨ櫕鐝ら梺鎸庣懆椤曘倖銇勯悽鍛婃〃闂佹彃绉撮悾楣冨触閹存繂鐓傞梻鍫熺懄閿熸垝绶氶妴澶愭閿燂拷 /50x.html\n"
				+ "    # 閻忓骏鎷� PHP 闁煎瓨纰嶅﹢鐗堢閿濆洦鍊為柛鎺炴嫹 127.0.0.1:80 闁烩晜鍨甸幆澶愭儍閿燂拷 Apache 濞戞搫鎷�\n"
				+ "    # 閻忓骏鎷� PHP 闁煎瓨纰嶅﹢鐗堝閻樹紮鎷烽幒鏃傝埗闁烩晜鍨甸幆锟� 127.0.0.1:9000 闁活煉鎷� FastCGI 闁哄牆绉存慨鐔煎闯閿燂拷\n"
				+ "    # 闁归攱甯炵划椋庢媼閸ф锛� .htaccess 闁哄倸娲ｅ▎銏ゆ晬鐏炵瓔娲ら柡瀣┚pache闁汇劌瀚弸鍐浖閿濆棛澹屽☉鎾崇獕ginx闁汇劌瀚粩鎾嚊閿燂拷\n"
				+ "  # 闁告瑱缂氱粩瀛樼▔椤忓洦鐝撻柟椋庡枍鐎靛矂寮甸悮瀵哥濞达綀娉曢弫銈夊春鏉炴壆鑹綢P闁靛棔绀侀幃鏇犵矓閺夋寧瀚茬紒鏃戝灠瑜版盯鎯冮崟顖氬赋缂傚喛鎷�\n"
				+ "  # HTTPS 闁哄牆绉存慨鐔煎闯閿燂拷\n"
				+ ""));
		System.out.println(isMessyCode("#user  nobody;\n"
				+ "    # 閻忓繐妫欏﹢鍥礉閳ヨ櫕鐝ら梺鎸庣懆椤曘倖銇勯悽鍛婃〃闂佹彃绉撮悾楣冨触閹存繂鐓傞梻鍫熺懄閿熸垝绶氶妴澶愭閿燂拷 /50x.html\n"
				+ ""));
		System.out.println(isMessyCode("#user  nobody;\n"
				+ "    # 閻忓骏鎷� PHP 闁煎瓨纰嶅﹢鐗堢閿濆洦鍊為柛鎺炴嫹 127.0.0.1:80 闁烩晜鍨甸幆澶愭儍閿燂拷 Apache 濞戞搫鎷�\n"
				+ ""));
		System.out.println(isMessyCode("#user  nobody;\n"
				+ "    # 閻忓骏鎷� PHP 闁煎瓨纰嶅﹢鐗堢閿濆洦鍊為柛鎺炴嫹 "
				+ ""));
		System.out.println(isMessyCode("#user  nobody;\n"
				+ "worker_processes 1;\n"
				+ "#error_log  logs/error.log;\n"
				+ "#error_log  logs/error.log  notice;\n"
				+ "#error_log  logs/error.log  info;\n"
				+ "#pid        logs/nginx.pid;\n"
				+ "events {\n"
				+ "  worker_connections 1024;\n"
				+ "}\n"
				+ "http {\n"
				+ "  include mime.types;\n"
				+ "  default_type application/octet-stream;\n"
				+ "  #log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n"
				+ "  #                  '$status $body_bytes_sent \"$http_referer\" '\n"
				+ "  #                  '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n"
				+ "  #access_log  logs/access.log  main;\n"
				+ "  sendfile on;\n"
				+ "  #tcp_nopush     on;\n"
				+ "  #keepalive_timeout  0;\n"
				+ "  keepalive_timeout 65;\n"
				+ "  gzip on;\n"
				+ "  server {\n"
				+ "    listen 80;\n"
				+ "    server_name localhost;\n"
				+ "    #charset koi8-r;\n"
				+ "    #access_log  logs/host.access.log  main;\n"
				+ "    location / {\n"
				+ "      root html;\n"
				+ "      index index.html index.htm;\n"
				+ "    }\n"
				+ "    #error_page  404              /404.html;\n"
				+ "    # ������������ҳ���ض��⵽¾�̬ҳ�� /50x.html\n"
				+ "    #\n"
				+ "    error_page 500 502 503 504 /50x.html;\n"
				+ "    location = /50x.html {\n"
				+ "      root html;\n"
				+ "    }\n"
				+ "    # �� PHP �ű����� 127.0.0.1:80 ������ Apache ��\n"
				+ "    #\n"
				+ "    #location ~ \\.php$ {\n"
				+ "    #    proxy_pass   http://127.0.0.1;\n"
				+ "    #}\n"
				+ "    # �� PHP �ű����ݸ����� 127.0.0.1:9000 �� FastCGI ������\n"
				+ "    #\n"
				+ "    #location ~ \\.php$ {\n"
				+ "    #    root           html;\n"
				+ "    #    fastcgi_pass   127.0.0.1:9000;\n"
				+ "    #    fastcgi_index  index.php;\n"
				+ "    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;\n"
				+ "    #    include        fastcgi_params;\n"
				+ "    #}\n"
				+ "    # �ܾ����� .htaccess �ļ������Apache���ĵ�����nginx��һ��\n"
				+ "    #\n"
				+ "    #location ~ /\\.ht {\n"
				+ "    #    deny  all;\n"
				+ "    #}\n"
				+ "  }\n"
				+ "  # ��һ������������ʹ�û���IP�����ƺͶ˿ڵ�����\n"
				+ "  #\n"
				+ "  #server {\n"
				+ "  #    listen       8000;\n"
				+ "  #    listen       somename:8080;\n"
				+ "  #    server_name  somename  alias  another.alias;\n"
				+ "  #    location / {-\n"
				+ "  #        root   html;\n"
				+ "  #        index  index.html index.htm;\n"
				+ "  #    }\n"
				+ "  #}\n"
				+ "  # HTTPS ������\n"
				+ "  #\n"
				+ "  #server {\n"
				+ "  #    listen       443 ssl;\n"
				+ "  #    server_name  localhost;\n"
				+ "  #    ssl_certificate      cert.pem;\n"
				+ "  #    ssl_certificate_key  cert.key;\n"
				+ "  #    ssl_session_cache    shared:SSL:1m;\n"
				+ "  #    ssl_session_timeout  5m;\n"
				+ "  #    ssl_ciphers  HIGH:!aNULL:!MD5;\n"
				+ "  #    ssl_prefer_server_ciphers  on;\n"
				+ "  #    location / {\n"
				+ "  #        root   html;\n"
				+ "  #        index  index.html index.htm;\n"
				+ "  #    }\n"
				+ "  #}\n"
				+ "}\n"
				+ ""));
		System.out.println(isMessyCode("#user  nobody;\n"
				+ "worker_processes 1;\n"
				+ "#error_log  logs/error.log;\n"
				+ "#error_log  logs/error.log  notice;\n"
				+ "#error_log  logs/error.log  info;\n"
				+ "#pid        logs/nginx.pid;\n"
				+ "events {\n"
				+ "  worker_connections 1024;\n"
				+ "}\n"
				+ "http {\n"
				+ "  include mime.types;\n"
				+ "  default_type application/octet-stream;\n"
				+ "  #log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n"
				+ "  #                  '$status $body_bytes_sent \"$http_referer\" '\n"
				+ "  #                  '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n"
				+ "  #access_log  logs/access.log  main;\n"
				+ "  sendfile on;\n"
				+ "  #tcp_nopush     on;\n"
				+ "  #keepalive_timeout  0;\n"
				+ "  keepalive_timeout 65;\n"
				+ "  #gzip  on;\n"
				+ "  # another virtual host using mix of IP-, name-, and port-based configuration\n"
				+ "  #\n"
				+ "  #server {\n"
				+ "  #    listen       8000;\n"
				+ "  #    listen       somename:8080;\n"
				+ "  #    server_name  somename  alias  another.alias;\n"
				+ "  #    location / {\n"
				+ "  #        root   html;\n"
				+ "  #        index  index.html index.htm;\n"
				+ "  #    }\n"
				+ "  #}\n"
				+ "  # HTTPS server\n"
				+ "  #\n"
				+ "  #server {\n"
				+ "  #    listen       443 ssl;\n"
				+ "  #    server_name  localhost;\n"
				+ "  #    ssl_certificate      cert.pem;\n"
				+ "  #    ssl_certificate_key  cert.key;\n"
				+ "  #    ssl_session_cache    shared:SSL:1m;\n"
				+ "  #    ssl_session_timeout  5m;\n"
				+ "  #    ssl_ciphers  HIGH:!aNULL:!MD5;\n"
				+ "  #    ssl_prefer_server_ciphers  on;\n"
				+ "  #    location / {\n"
				+ "  #        root   html;\n"
				+ "  #        index  index.html index.htm;\n"
				+ "  #    }\n"
				+ "  #}\n"
				+ "  server {\n"
				+ "    listen 80;\n"
				+ "    server_name localhost;\n"
				+ "    #charset koi8-r;\n"
				+ "    #access_log  logs/host.access.log  main;\n"
				+ "    location / {\n"
				+ "      root html;\n"
				+ "      index index.html index.htm;\n"
				+ "      proxy_pass http://localhost:81;\n"
				+ "    }\n"
				+ "    #error_page  404              /404.html;\n"
				+ "    # redirect server error pages to the static page /50x.html\n"
				+ "    #\n"
				+ "    error_page 500 502 503 504 /50x.html;\n"
				+ "    location = /50x.html {\n"
				+ "      root html;\n"
				+ "    }\n"
				+ "    # proxy the PHP scripts to Apache listening on 127.0.0.1:80\n"
				+ "    #\n"
				+ "    #location ~ \\.php$ {\n"
				+ "    #    proxy_pass   http://127.0.0.1;\n"
				+ "    #}\n"
				+ "    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000\n"
				+ "    #\n"
				+ "    #location ~ \\.php$ {\n"
				+ "    #    root           html;\n"
				+ "    #    fastcgi_pass   127.0.0.1:9000;\n"
				+ "    #    fastcgi_index  index.php;\n"
				+ "    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;\n"
				+ "    #    include        fastcgi_params;\n"
				+ "    #}\n"
				+ "    # deny access to .htaccess files, if Apache's document root\n"
				+ "    # concurs with nginx's one\n"
				+ "    #\n"
				+ "    #location ~ /\\.ht {\n"
				+ "    #    deny  all;\n"
				+ "    #}\n"
				+ "  }\n"
				+ "}\n"
				+ ""));
	}
}
